<?php
/**
 * Created By PhpStorm
 * Author: patrick wu
 * Date: 2020/9/24
 * Time: 18:15
 */
namespace App\Service\Admin;

use App\Http\Model\Mysql\ExpressModel;

class ExpressService extends BaseService
{
    /**
     * 快递公司列表
     * @return array
     */
    public function expressList()
    {
        $list   =   ExpressModel::where(['is_delete' => ExpressModel::NOT_DELETE])->select(['id', 'express_name', 'express_web', 'express_phone', 'short_name'])->orderBy('id', 'desc')->get();
        if ($list->isEmpty()) {
            return [true, []];
        } else {
            return [true, $list->toArray()];
        }
    }

    /**
     * 快递公司添加
     * @param array $params  添加参数
     * @return array
     */
    public function expressAdd(array $params)
    {
        $insert['unique_id']    =   createUniqueID();
        $insert['express_name'] =   $params['express_name'];
        $insert['short_name']   =   $params['short_name'] ?? '';
        $insert['express_web']  =   $params['express_web'] ?? '';
        $insert['express_phone']=   $params['express_phone'] ?? '';
        $insert['is_delete']    =   ExpressModel::NOT_DELETE;

        $insert_result  =   ExpressModel::create($insert);
        if ($insert_result) {
            return [true, '添加成功'];
        } else {
            return [false, '添加失败'];
        }
    }


    /**
     * 快递公司编辑
     * @param array $params
     * @param int $express_id
     * @return array
     */
    public function expressEdit(array $params, int $express_id)
    {
        $update['express_name'] =   $params['express_name'];
        $update['short_name']   =   $params['short_name'] ?? '';
        $update['express_web']  =   $params['express_web'] ?? '';
        $update['express_phone']=   $params['express_phone'] ?? '';

        $update_result  =   ExpressModel::where(['id' => $express_id])->update($update);
        if ($update_result) {
            return [true, '修改成功'];
        } else {
            return [false, '修改失败'];
        }
    }

    /**
     * 快递公司删除
     * @param int $express_id
     * @return array
     */
    public function expressDelete(int $express_id)
    {
        $delete_result  =   ExpressModel::where(['id' => $express_id])->update(['is_delete' => ExpressModel::IS_DELETE]);
        if ($delete_result) {
            return [true, '删除成功'];
        } else {
            return [false, '删除失败'];
        }
    }


}
